package com.jinhu.Service;

import cn.hutool.core.util.ObjUtil;
import com.jinhu.Entity.wxuserinfo;
import com.jinhu.ResultEntity;
import net.hasor.dbvisitor.jdbc.core.JdbcTemplate;
import net.hasor.dbvisitor.lambda.LambdaTemplate;
import net.hasor.dbvisitor.solon.Db;
import org.noear.solon.annotation.Component;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

@Component
public class AccountServiceImpl implements IAccountService{
    @Db
    JdbcTemplate jdbcTemplate;
    @Db
    LambdaTemplate lambdaTemplate;

    @Override
    public wxuserinfo login(String openid) {
        try {
            wxuserinfo wxuserinfo = jdbcTemplate.queryForObject("select * from wxuserinfo", wxuserinfo.class);
            if(ObjUtil.isEmpty(wxuserinfo)) return null;
            return wxuserinfo;
        } catch (SQLException e) {
            throw new RuntimeException("没有找到用户"+e);
        }
    }

    @Override
    public wxuserinfo register(String openid) {
        wxuserinfo wx = new wxuserinfo();
        wx.setNickName("微信用户"+ UUID.randomUUID());
        wx.setGender(0);
        wx.setMobile(null);
        wx.setToken(null);
        wx.setOpenid(openid);
        try {
            int i = lambdaTemplate.lambdaInsert(wxuserinfo.class).applyEntity(wx).executeSumResult();
            if(i>0){
                return wx;
            }else {
                return null;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public wxuserinfo getUserInfo(String openid) {
        return null;
    }
}
